<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>磨锋小程序管理平台</title>
	<link rel="stylesheet" href="./resources/libraries/element-plus@2.6.0/index.css" />
	<link rel="stylesheet" href="./resources/css/frame.css">
	<link rel="stylesheet" href="./resources/css/page.css">
	<link ref="stylesheet" href="./resources/libraries/wangeditor@5.1.23/editor/dist/css/style.css">
	<link rel="stylesheet" href="./resources/css/miniapp.css">
	<script src="./global.js"></script>
	<!--
	<script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script>
	-->
</head>

<body>
	<div id="frame">
		<router-view v-if="useRouter"></router-view>
	</div>
	<script>
		//var vConsole = new window.VConsole();
	</script>
	<script type="importmap">
    {
      "imports": {
        "lib/": "./resources/libraries/",
        "comp/": "./components/",
        "comm/": "./resources/common/",
        "utils": "./resources/common/utils.js",
        "vue": "./resources/libraries/vue@3.3.4/vue.esm-browser.js",
        "element-plus": "./resources/libraries/element-plus@2.6.0/index.full.min.mjs",
        "vue-router": "./resources/libraries/vue-router@4.2.5/vue-router.esm-browser.js",
        "moment": "./resources/libraries/moment@2.29.4/dist/moment.js",
        "lodash": "./resources/libraries/lodash/dist/lodash-esm.js",
        "wovoui-icons": "./resources/libraries/wovoui-icons@1.1.9/index.es.mjs",
        "pinia": "./resources/dist/pinia-esm.js",
        "mf-components": "./components/mf-components.js"
      }
    }
  </script>
	<script type="module">

		import { createApp, provide, ref, reactive } from 'vue';
		import ElementPlus, { ElMessage } from 'element-plus';
		import zhCn from './resources/libraries/element-plus@2.6.0/locale/zh-cn.mjs';
		import { createPinia } from 'pinia';
		import * as ElementPlusIconsVue from './resources/libraries/element-plus-icons-vue@2.1.0/index.js';
		import { mofRouter } from 'comm/router.js';

		import http from 'comm/http.js';
		import { getConfig } from 'comm/config.js';
		import { isLogin } from 'comm/userLogin.js';
		import { useUserStore } from 'comm/userStore.js';
		import { useRouteStore } from 'comm/routeStore.js';

		const router = mofRouter('miniapp', [{
			name: 'index',
			path: '/index',
			meta: { title: '磨锋小程序管理平台', default: true },
			component: () => import(`./app/miniapp/index.js`)
		}]);
		const pinia = createPinia();
		const app = createApp({
			setup() {
				const miniapp = ref({});
				const useRouter = ref(false);

				const loginModule = 'miniapp';
				const loginTitle = ref('磨锋小程序管理平台');

				const setMiniapp = (res) => {
					//更新网页标题
					document.title = `${res.data.title} - 磨锋小程序管理平台`;
					miniapp.value = res.data;
					miniapp.value.avatar_img = miniapp.value.avatar_img || './resources/images/avatar.jpg';
					useRouteStore().setPerms(res.data.perms);
				}

				const getPagePath = () => {
					let path = '/miniapp/statistics';
					if (window.location.hash.length > 2) {
						path = window.location.hash.substring(1);
						if (!path || path === '/index' || !router.hasRoutePath(path)) {
							path = '/miniapp/statistics';
						}
					}
					return path;
				}

				//依赖注入
				provide('http', http);
				provide('miniapp', miniapp);
				//登录用
				provide('loginModule', loginModule);
				provide('loginTitle', loginTitle);

				getConfig().then(() => {
					//登录状态
					isLogin('miniapp').then(res => {
						return http.get('/system/passport/info?module=miniapp');
					}).then(async (res) => {
						//设置用户信息
						useUserStore().setUser(res.data.user);
						let pagePath = '/index';
						//判断是否指定了小程序平台id
						const urlParams = new URLSearchParams(window.location.search);
						const id = urlParams.get('id');
						if (id && /[0-9]+/.test(id)) {
							try {
								//获取小程序平台信息
								setMiniapp(await http.get(`/miniapp/backend/index/${id}`));
								pagePath = getPagePath();
							} catch (err) {
								err.message && ElMessage.error(err.message);
								pagePath = '/index';
							}
						}
						console.log('pagePath---', pagePath)
						router.push(pagePath);
					}).catch(err => {
						err && console.error(err);
						if (router.currentRoute.value.path !== '/login') {
							router.push('/login');
						}
					}).finally(() => {
						useRouter.value = true;
					});
				}).catch(err => {
					console.log(err);
					err.errmsg && ElMessage.error(err.errmsg);
				});

				return {
					useRouter
				};
			}
		});

		app.use(ElementPlus, { locale: zhCn, }).use(router).use(pinia);
		for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
			app.component(key, component);
		}
		app.mount('#frame');
	</script>
</body>

</html>